<!DOCTYPE html>

<html>
<head>
  <title>database.coffee</title>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <meta name="viewport" content="width=device-width, target-densitydpi=160dpi, initial-scale=1.0; maximum-scale=1.0; user-scalable=0;">
  <link rel="stylesheet" media="all" href="docco.css" />
</head>
<body>
  <div id="container">
    <div id="background"></div>
    
      <ul id="jump_to">
        <li>
          <a class="large" href="javascript:void(0);">Jump To &hellip;</a>
          <a class="small" href="javascript:void(0);">+</a>
          <div id="jump_wrapper">
          <div id="jump_page">
            
              
              <a class="source" href="configure.html">
                configure.coffee
              </a>
            
              
              <a class="source" href="dataParser.html">
                dataParser.coffee
              </a>
            
              
              <a class="source" href="database.html">
                database.coffee
              </a>
            
              
              <a class="source" href="logger.html">
                logger.coffee
              </a>
            
              
              <a class="source" href="mail.html">
                mail.coffee
              </a>
            
              
              <a class="source" href="messages.html">
                messages.coffee
              </a>
            
              
              <a class="source" href="routes.html">
                routes.coffee
              </a>
            
              
              <a class="source" href="settings.html">
                settings.coffee
              </a>
            
              
              <a class="source" href="settings.default.html">
                settings.default.coffee
              </a>
            
              
              <a class="source" href="twitter.html">
                twitter.coffee
              </a>
            
          </div>
        </li>
      </ul>
    
    <ul class="sections">
        
          <li id="title">
              <div class="annotation">
                  <h1>database.coffee</h1>
              </div>
          </li>
        
        
        
        <li id="section-1">
            <div class="annotation">
              
              <div class="pilwrap for-h2">
                <a class="pilcrow" href="#section-1">&#182;</a>
              </div>
              <h2>SERVER DATABASE</h2>

            </div>
            
        </li>
        
        
        <li id="section-2">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-2">&#182;</a>
              </div>
              <p>Handle the MongoDB database interactions on the app.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre><span class="class"><span class="keyword">class</span> <span class="title">Database</span></span>

    logger = require <span class="string">"./logger.coffee"</span>
    settings = require <span class="string">"./settings.coffee"</span>
    mongo = require <span class="string">"mongoskin"</span></pre></div></div>
            
        </li>
        
        
        <li id="section-3">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-3">&#182;</a>
              </div>
              <p>Database object, will be set during <code>init</code>.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    db = <span class="literal">null</span></pre></div></div>
            
        </li>
        
        
        <li id="section-4">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-4">&#182;</a>
              </div>
              <p>When using the failover databse, <code>failover</code> will be set to true.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    failover: <span class="literal">false</span></pre></div></div>
            
        </li>
        
        
        <li id="section-5">
            <div class="annotation">
              
              <div class="pilwrap for-h2">
                <a class="pilcrow" href="#section-5">&#182;</a>
              </div>
              <h2>INIT AND MANAGEMENT</h2>

            </div>
            
        </li>
        
        
        <li id="section-6">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-6">&#182;</a>
              </div>
              <p>Init the databse by testing the connection. If connection fails multiple times in a row,
switch to the failover DB (specified on the <code>Database.connString2</code> setting).
You can customize these values on the <code>Database</code> <a href="settings.html">settings</a>.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    init: (retry) =&gt;
        retry = <span class="number">0</span> <span class="keyword">if</span> <span class="keyword">not</span> retry?</pre></div></div>
            
        </li>
        
        
        <li id="section-7">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-7">&#182;</a>
              </div>
              <p>If connection has failed repeatedly for more than 10 times the <code>maxRetries</code> value then
stop trying and log an error.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>        <span class="keyword">if</span> retry &gt; settings.Database.maxRetries * <span class="number">10</span>
            logger.error <span class="string">"Database.init"</span>, <span class="string">"Connection failed <span class="subst">#{retry}</span> times."</span>, <span class="string">"Quit trying!"</span>
            <span class="keyword">return</span></pre></div></div>
            
        </li>
        
        
        <li id="section-8">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-8">&#182;</a>
              </div>
              <p>First try, use main database.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>        <span class="keyword">if</span> retry &lt; <span class="number">1</span>
            db = mongo.db settings.Database.connString, settings.Database.options
            <span class="property">@failover</span> = <span class="literal">false</span></pre></div></div>
            
        </li>
        
        
        <li id="section-9">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-9">&#182;</a>
              </div>
              <p>Reached max retries? Try connecting to the failover database, if there&#39;s one specified.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>        <span class="keyword">if</span> retry <span class="keyword">is</span> settings.Database.maxRetries
            <span class="keyword">if</span> settings.Database.connString2? <span class="keyword">and</span> settings.Database.connString2 <span class="keyword">isnt</span> <span class="string">""</span>
                <span class="property">@failover</span> = <span class="literal">true</span>
                db = mongo.db settings.Database.connString2, settings.Database.options
                logger.info <span class="string">"Database.init"</span>, <span class="string">"Connection failed <span class="subst">#{retry}</span> times."</span>, <span class="string">"Switched to failover database."</span>
            <span class="keyword">else</span>
                logger.error <span class="string">"Database.init"</span>, <span class="string">"Connection failed <span class="subst">#{retry}</span> times."</span>, <span class="string">"No failover database set, keep trying."</span></pre></div></div>
            
        </li>
        
        
        <li id="section-10">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-10">&#182;</a>
              </div>
              <p>Try to connect to the current database. If it fails, try again in a few seconds.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>        db.open (err, result) =&gt;
            <span class="keyword">if</span> err?
                <span class="keyword">if</span> settings.General.debug
                    logger.warn <span class="string">"Database.init"</span>, <span class="string">"Failed to connect."</span>, <span class="string">"Retry <span class="subst">#{retry}</span>."</span>
                setTimeout (() -&gt; <span class="property">@init</span> retry + <span class="number">1</span>), settings.Database.retryInterval
            <span class="keyword">else</span> <span class="keyword">if</span> settings.General.debug</pre></div></div>
            
        </li>
        
        
        <li id="section-11">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-11">&#182;</a>
              </div>
              <p>If using the failover database, register a timeout to try
to connect to the main database again.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>                <span class="keyword">if</span> <span class="property">@failover</span>
                    setTimeout <span class="property">@init</span>, settings.Database.failoverTimeout * <span class="number">1000</span>
                    logger.info <span class="string">"Database.init"</span>, <span class="string">"Connected to failover database."</span>, <span class="string">"Try the main again in <span class="subst">#{settings.Database.failoverTimeout}</span> settings."</span>
                <span class="keyword">else</span>
                    logger.info <span class="string">"Database.init"</span>, <span class="string">"Connected to main database."</span></pre></div></div>
            
        </li>
        
        
        <li id="section-12">
            <div class="annotation">
              
              <div class="pilwrap for-h2">
                <a class="pilcrow" href="#section-12">&#182;</a>
              </div>
              <h2>LOW LEVEL IMPLEMENTATION</h2>

            </div>
            
        </li>
        
        
        <li id="section-13">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-13">&#182;</a>
              </div>
              <p>Get data from the database. A <code>collection</code> must be specified.
The <code>options</code> and <code>callback</code> are optional.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    get: (collection, options, callback) =&gt;
        <span class="keyword">if</span> <span class="keyword">not</span> callback?
            logger.warn <span class="string">"Database.get"</span>, <span class="string">"No callback specified. Abort!"</span>, collection, options
            <span class="keyword">return</span></pre></div></div>
            
        </li>
        
        
        <li id="section-14">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-14">&#182;</a>
              </div>
              <p>Create the DB callback helper.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>        <span class="function"><span class="title">dbCallback</span></span> = (err, result) =&gt;
            <span class="keyword">if</span> callback?
                result = <span class="property">@normalizeId</span> result
                callback err, result</pre></div></div>
            
        </li>
        
        
        <li id="section-15">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-15">&#182;</a>
              </div>
              <p>Find documents depending on the options.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>        dbCollection = db.collection collection
        <span class="keyword">if</span> options?
            <span class="keyword">if</span> options.id?
                dbCollection.findById options.id, dbCallback
            <span class="keyword">else</span>
                dbCollection.find(options).toArray dbCallback
        <span class="keyword">else</span>
            dbCollection.find().toArray dbCallback</pre></div></div>
            
        </li>
        
        
        <li id="section-16">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-16">&#182;</a>
              </div>
              <p>Log if debug is true.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>        <span class="keyword">if</span> settings.General.debug
            logger.info <span class="string">"Database.get"</span>, collection, options</pre></div></div>
            
        </li>
        
        
        <li id="section-17">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-17">&#182;</a>
              </div>
              <p>Insert or update an object on the database.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    set: (collection, obj, callback) =&gt;
        <span class="keyword">if</span> <span class="keyword">not</span> obj?
            msg = <span class="string">"The obj argument is null or empty."</span>
            callback msg, <span class="literal">null</span>
            logger.warn <span class="string">"Database.set"</span>, msg
            <span class="keyword">return</span></pre></div></div>
            
        </li>
        
        
        <li id="section-18">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-18">&#182;</a>
              </div>
              <p>Create the DB callback helper.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>        <span class="function"><span class="title">dbCallback</span></span> = (err, result) =&gt;
            <span class="keyword">if</span> callback?
                result = <span class="property">@normalizeId</span> result
                callback err, result</pre></div></div>
            
        </li>
        
        
        <li id="section-19">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-19">&#182;</a>
              </div>
              <p>If object already has an ID, update it otherwise insert a new one.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>        dbCollection = db.collection collection
        <span class="keyword">if</span> obj.id?
            dbCollection.updateById obj.id, {$set: obj.attributes}, dbCallback
        <span class="keyword">else</span>
            dbCollection.insert obj.attributes, {<span class="string">"new"</span>: <span class="literal">true</span>}, dbCallback</pre></div></div>
            
        </li>
        
        
        <li id="section-20">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-20">&#182;</a>
              </div>
              <p>Log if debug is true.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>        <span class="keyword">if</span> settings.General.debug
            logger.info <span class="string">"Database.set"</span>, collection, obj.id, obj.attributes</pre></div></div>
            
        </li>
        
        
        <li id="section-21">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-21">&#182;</a>
              </div>
              <p>Delete an object from the database. The <code>obj</code> argument can be either the object
itself, or its integer/string ID.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    del: (collection, obj, callback) =&gt;
        <span class="keyword">if</span> <span class="keyword">not</span> obj?
            msg = <span class="string">"The obj argument is null or empty."</span>
            callback msg, <span class="literal">null</span>
            logger.warn <span class="string">"Database.del"</span>, msg
            <span class="keyword">return</span></pre></div></div>
            
        </li>
        
        
        <li id="section-22">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-22">&#182;</a>
              </div>
              <p>Check it the <code>obj</code> is the model itself, or only the ID string / number.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>        <span class="keyword">if</span> obj.id?
            id = obj.id
        <span class="keyword">else</span>
            id = obj</pre></div></div>
            
        </li>
        
        
        <li id="section-23">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-23">&#182;</a>
              </div>
              <p>Create the DB callback helper.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>        <span class="function"><span class="title">dbCallback</span></span> = (err, result) =&gt;
            <span class="keyword">if</span> callback?
                result = <span class="property">@normalizeId</span> result
                callback err, result</pre></div></div>
            
        </li>
        
        
        <li id="section-24">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-24">&#182;</a>
              </div>
              <p>Remove specified document from the database.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>        dbCollection = db.collection collection
        dbCollection.removeById id, dbCallback</pre></div></div>
            
        </li>
        
        
        <li id="section-25">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-25">&#182;</a>
              </div>
              <p>Log if debug is true.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>        <span class="keyword">if</span> settings.General.debug
            logger.warn <span class="string">"Database.del"</span>, collection, obj.id, obj.attributes</pre></div></div>
            
        </li>
        
        
        <li id="section-26">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-26">&#182;</a>
              </div>
              <p>Count data from the database. A <code>collection</code> must be specified.
If no <code>options</code> is passed (null or undefined) then count all documents.
The callback is mandatory.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    count: (collection, options, callback) =&gt;
        <span class="keyword">if</span> <span class="keyword">not</span> callback?
            logger.warn <span class="string">"Database.count"</span>, <span class="string">"No callback specified. Abort!"</span>, collection, options
            <span class="keyword">return</span></pre></div></div>
            
        </li>
        
        
        <li id="section-27">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-27">&#182;</a>
              </div>
              <p>Create the DB callback helper.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>        <span class="function"><span class="title">dbCallback</span></span> = (err, result) =&gt;
            <span class="keyword">if</span> callback?
                callback err, result
                <span class="keyword">if</span> settings.General.debug
                    logger.info <span class="string">"Database.count"</span>, collection, options, result</pre></div></div>
            
        </li>
        
        
        <li id="section-28">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-28">&#182;</a>
              </div>
              <p>MongoDB has a built-in count, so use it.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>        dbCollection = db.collection collection
        dbCollection.count options, dbCallback</pre></div></div>
            
        </li>
        
        
        <li id="section-29">
            <div class="annotation">
              
              <div class="pilwrap for-h2">
                <a class="pilcrow" href="#section-29">&#182;</a>
              </div>
              <h2>COUNTDOWN METHODS</h2>

            </div>
            
        </li>
        
        
        <li id="section-30">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-30">&#182;</a>
              </div>
              <p>Get a collection of <a href="countdown.html">Countdown</a> models, based on the passed options.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    getCountdown: (options, callback) =&gt;
        collection = <span class="string">"countdown"</span>
        <span class="property">@get</span> collection, options, callback</pre></div></div>
            
        </li>
        
        
        <li id="section-31">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-31">&#182;</a>
              </div>
              <p>Insert a new <a href="countdown.html">Countdown</a>. The <code>dateLevel</code> will determine how
many parts of the <code>targetDate</code> will be added to the generate friendly URL.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    insertCountdown: (obj, callback, dateLevel) =&gt;
        collection = <span class="string">"countdown"</span>
        dateLevel = <span class="number">0</span> <span class="keyword">if</span> <span class="keyword">not</span> dateLevel?
        obj.generateFriendlyUrl dateLevel

        <span class="property">@get</span> collection, {friendlyUrl: obj.get <span class="string">"friendlyUrl"</span>}, (err, result) =&gt;
            <span class="keyword">if</span> result?.length &gt; <span class="number">0</span>
                <span class="property">@insertCountdown</span> obj, callback, dateLevel + <span class="number">1</span>
            <span class="keyword">else</span>
                <span class="property">@set</span> collection, obj, callback</pre></div></div>
            
        </li>
        
        
        <li id="section-32">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-32">&#182;</a>
              </div>
              <p>Update an existing <a href="countdown.html">Countdown</a>.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    updateCountdown: (obj, callback) =&gt;
        collection = <span class="string">"countdown"</span>
        <span class="property">@set</span> collection, obj, callback</pre></div></div>
            
        </li>
        
        
        <li id="section-33">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-33">&#182;</a>
              </div>
              <p>Delete a <a href="countdown.html">Countdown</a>.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    deleteCountdown: (id, callback) =&gt;
        collection = <span class="string">"countdown"</span>
        <span class="property">@del</span> collection, id, callback</pre></div></div>
            
        </li>
        
        
        <li id="section-34">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-34">&#182;</a>
              </div>
              <p>Count how many <a href="countdown.html">Countdowns</a> are registered.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    countCountdown: (options, callback) =&gt;
        collection = <span class="string">"countdown"</span>
        <span class="property">@count</span> collection, options, callback</pre></div></div>
            
        </li>
        
        
        <li id="section-35">
            <div class="annotation">
              
              <div class="pilwrap for-h2">
                <a class="pilcrow" href="#section-35">&#182;</a>
              </div>
              <h2>EGG TIMER METHODS</h2>

            </div>
            
        </li>
        
        
        <li id="section-36">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-36">&#182;</a>
              </div>
              <p>Get a collection of <a href="eggTimer.html">Egg Timer</a> models, based on the passed options.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    getEggTimer: (options, callback) =&gt;
        collection = <span class="string">"eggtimer"</span>
        <span class="property">@get</span> collection, options, callback</pre></div></div>
            
        </li>
        
        
        <li id="section-37">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-37">&#182;</a>
              </div>
              <p>Insert a new <a href="eggTimer.html">Egg Timer</a>.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    insertEggTimer: (obj, callback) =&gt;
        collection = <span class="string">"eggtimer"</span>
        <span class="property">@set</span> collection, obj, callback</pre></div></div>
            
        </li>
        
        
        <li id="section-38">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-38">&#182;</a>
              </div>
              <p>Update an existing <a href="eggTimer.html">Egg Timer</a>.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    updatedEggTimer: (obj, callback) =&gt;
        collection = <span class="string">"eggtimer"</span>
        <span class="property">@set</span> collection, obj, callback</pre></div></div>
            
        </li>
        
        
        <li id="section-39">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-39">&#182;</a>
              </div>
              <p>Delete a <a href="eggTimer.html">Egg Timer</a>.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    deleteEggTimer: (id, callback) =&gt;
        collection = <span class="string">"eggtimer"</span>
        <span class="property">@del</span> collection, id, callback</pre></div></div>
            
        </li>
        
        
        <li id="section-40">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-40">&#182;</a>
              </div>
              <p>Count how many <a href="eggTimer.html">Egg Timers</a> are registered.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    countEggTimer: (options, callback) =&gt;
        collection = <span class="string">"eggtimer"</span>
        <span class="property">@count</span> collection, options, callback</pre></div></div>
            
        </li>
        
        
        <li id="section-41">
            <div class="annotation">
              
              <div class="pilwrap for-h2">
                <a class="pilcrow" href="#section-41">&#182;</a>
              </div>
              <h2>SCHEDULED TWITTER METHODS</h2>

            </div>
            
        </li>
        
        
        <li id="section-42">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-42">&#182;</a>
              </div>
              <p>Get a collection of <a href="scheduledTwitter.html">Scheduled Twitter Update</a> models, based on the passed options.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    getScheduledTwitter: (options, callback) =&gt;
        collection = <span class="string">"scheduledtwitter"</span>
        <span class="property">@get</span> collection, options, callback</pre></div></div>
            
        </li>
        
        
        <li id="section-43">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-43">&#182;</a>
              </div>
              <p>Insert a new <a href="scheduledTwitter.html">Scheduled Twitter Update</a>.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    insertScheduledTwitter: (obj, callback) =&gt;
        collection = <span class="string">"scheduledtwitter"</span>
        <span class="property">@set</span> collection, obj, callback</pre></div></div>
            
        </li>
        
        
        <li id="section-44">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-44">&#182;</a>
              </div>
              <p>Update an existing <a href="scheduledTwitter.html">Scheduled Twitter Update</a>.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    updateScheduledTwitter: (obj, callback) =&gt;
        collection = <span class="string">"scheduledtwitter"</span>
        <span class="property">@set</span> collection, obj, callback</pre></div></div>
            
        </li>
        
        
        <li id="section-45">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-45">&#182;</a>
              </div>
              <p>Delete a <a href="scheduledTwitter.html">Scheduled Twitter Update</a>.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    deleteScheduledTwitter: (id, callback) =&gt;
        collection = <span class="string">"scheduledtwitter"</span>
        <span class="property">@del</span> collection, id, callback</pre></div></div>
            
        </li>
        
        
        <li id="section-46">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-46">&#182;</a>
              </div>
              <p>Count how many <a href="scheduledTwitter.html">Scheduled Twitter Updates</a> are registered.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    countScheduledTwitter: (options, callback) =&gt;
        collection = <span class="string">"scheduledtwitter"</span>
        <span class="property">@count</span> collection, options, callback</pre></div></div>
            
        </li>
        
        
        <li id="section-47">
            <div class="annotation">
              
              <div class="pilwrap for-h2">
                <a class="pilcrow" href="#section-47">&#182;</a>
              </div>
              <h2>HELPER METHODS</h2>

            </div>
            
        </li>
        
        
        <li id="section-48">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-48">&#182;</a>
              </div>
              <p>Make sure that we transform MongoDB &quot;_id&quot; to &quot;id&quot;.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    normalizeId: (result) =&gt;
        <span class="keyword">if</span> <span class="keyword">not</span> result?
            <span class="keyword">return</span>

        <span class="keyword">if</span> result.length?
            <span class="keyword">for</span> obj <span class="keyword">in</span> result
                obj[<span class="string">"id"</span>] = obj[<span class="string">"_id"</span>].toString()
                <span class="keyword">delete</span> obj[<span class="string">"_id"</span>]
        <span class="keyword">else</span>
            result[<span class="string">"id"</span>] = result[<span class="string">"_id"</span>].toString()
            <span class="keyword">delete</span> result[<span class="string">"_id"</span>]

        <span class="keyword">return</span> result</pre></div></div>
            
        </li>
        
        
        <li id="section-49">
            <div class="annotation">
              
              <div class="pilwrap for-h2">
                <a class="pilcrow" href="#section-49">&#182;</a>
              </div>
              <h2>Singleton implementation.</h2>

            </div>
            
        </li>
        
        
        <li id="section-50">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-50">&#182;</a>
              </div>
              
            </div>
            
            <div class="content"><div class='highlight'><pre>Database.<span class="function"><span class="title">getInstance</span></span> = -&gt;
    <span class="property">@instance</span> = <span class="keyword">new</span> Database() <span class="keyword">if</span> <span class="keyword">not</span> <span class="property">@instance</span>?
    <span class="keyword">return</span> <span class="property">@instance</span>

module.exports = exports = Database.getInstance()</pre></div></div>
            
        </li>
        
    </ul>
  </div>
</body>
</html>
